﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace StringFormateHelper
{


    public class IsNullCheck
    {
        public bool IsNull(object Value)
        {
            if (Value != null)
                return Value.ToString() == string.Empty ? true : false;
            else
                return false;
        }
    }

    public class StringOperation : IsNullCheck
    {
        private string GetRegExp(EnumCollection.StringFormat stringActionType)
        {
            string returnValue = null;
            switch (stringActionType)
            {
                case EnumCollection.StringFormat.RSpecialChar:
                    {
                        returnValue = "[^a-zA-Z0-9]+"; //Remove All Special Character
                        break;
                    }
                case EnumCollection.StringFormat.RAllAlphabet:
                    {
                        returnValue = "[^0-9.]+"; //Remove All Alphabet
                        break;
                    }
                case EnumCollection.StringFormat.RNumber:
                    {
                        returnValue = "[^a-zA-Z -]+"; //Remove All Number
                        break;
                    }
                case EnumCollection.StringFormat.RUpperAlphaNumeric:
                    {
                        returnValue = "[^a-z -]+"; //Remove All Lower and Number
                        break;
                    }
                case EnumCollection.StringFormat.RLowerAlphaNumeric:
                    {
                        returnValue = "[^A-Z -]+"; //Remove All Upper and Number
                        break;
                    }
                case EnumCollection.StringFormat.RUpperAlphabet:
                    {
                        returnValue = "[^a-z0-9 -]+"; //Remove All Lower
                        break;
                    }
                case EnumCollection.StringFormat.RLowerAlphabet:
                    {
                        returnValue = "[^A-Z0-9 -]+"; //Remove All Upper
                        break;
                    }
                case EnumCollection.StringFormat.VEmail:
                    {
                        //returnValue = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
                        returnValue = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                        break;
                    }
                case EnumCollection.StringFormat.CDecimal:
                    {
                        returnValue = "[^0-9.]+"; //Return Decimal format
                        break;
                    }
                case EnumCollection.StringFormat.CNumber:
                    {
                        returnValue = "[^0-9]+"; //Return Decimal format
                        break;
                    }

            }
            return returnValue;
        }

        private string GetOutPutExp(EnumCollection.StringFormat stringActionType)
        {
            string returnValue = null;
            switch (stringActionType)
            {
                case EnumCollection.StringFormat.RAllAlphabet:
                    {
                        returnValue = "${month}/${day}/${year}";
                        break;
                    }
            }
            return returnValue;
        }

        public dynamic GetString(string Value, EnumCollection.StringFormat stringActionType, EnumCollection.ActionType actionType)
        {
            object returnValue = null;

            switch (actionType)
            {
                case EnumCollection.ActionType.Replace:
                    {
                        returnValue = IsNull(Value) ? Value : Regex.Replace(Value.Trim(), GetRegExp(stringActionType), "", RegexOptions.Compiled);
                        break;
                    }
                case EnumCollection.ActionType.MatchPattern:
                    {
                        var regEx = new Regex(GetRegExp(stringActionType));
                        returnValue = IsNull(Value) ? false : regEx.IsMatch(Value);
                        break;
                    }
                
            }

            return returnValue;
        }
    }

    public class EnumCollection
    {
        /// <summary>
        /// First Letter Shows Action type eg.
        /// R: Remove, V: Validate, C: Convert
        /// </summary>
        public enum StringFormat
        {
            RSpecialChar,
            RAllAlphabet,
            RNumber,
            RUpperAlphaNumeric,
            RLowerAlphaNumeric,
            RUpperAlphabet,
            RLowerAlphabet,
            CCapitalize,
            KeepSpace,
            VEmail,
            CDecimal,
            CNumber,
        }
        public enum ActionType
        {
            MatchPattern,
            Replace
        }
    }

    public static class StringExtantion
    {

        static IsNullCheck isNullCheck = new IsNullCheck();
        public static decimal ToDecimal(this string Value)
        {
            object cValue = "0" + new StringOperation().GetString(Value.ToString(), EnumCollection.StringFormat.CDecimal, EnumCollection.ActionType.Replace);
            decimal returnValue = isNullCheck.IsNull(Value) ? 0m : Convert.ToDecimal(isNullCheck.IsNull(cValue) ? 0m : cValue);
            return returnValue;
        }
        public static double ToDouble(this string Value)
        {
            object cValue = "0" + new StringOperation().GetString(Value.ToString(), EnumCollection.StringFormat.CDecimal, EnumCollection.ActionType.Replace);
            double returnValue = isNullCheck.IsNull(Value) ? 0 : Convert.ToDouble(isNullCheck.IsNull(cValue) ? 0m : cValue);
            return returnValue;
        }
        public static int ToInt(this string Value)
        {
            object cValue = "0" + new StringOperation().GetString(Value.ToString(), EnumCollection.StringFormat.CDecimal, EnumCollection.ActionType.Replace);
            int returnValue = isNullCheck.IsNull(Value) ? 0 : Convert.ToInt32(cValue.ToString().ToDecimal());
            return returnValue;
        }
        public static long ToBigInt(this string Value)
        {
            object cValue = "0" + new StringOperation().GetString(Value.ToString(), EnumCollection.StringFormat.CDecimal, EnumCollection.ActionType.Replace);
            long returnValue = isNullCheck.IsNull(Value) ? 0 : Convert.ToInt64(cValue.ToString().ToDecimal());
            return returnValue;
        }
    }


}